package com.dianxin.smartpark.service.park.impl;

import java.util.Date;
import java.util.List;

import com.chinatelecom.response.common.CommonResponseBody;
import com.chinatelecom.response.utils.BeanUtil;
import com.chinatelecom.response.utils.Convert;
import com.chinatelecom.response.utils.ResponseBodyWrapperUtil;
import com.chinatelecom.response.constant.ErrMsgEnum;

import com.chinatelecom.response.utils.StringUtils;
import com.dianxin.smartpark.dao.dataobject.park.SmartParkCarParkPlace;
import com.dianxin.smartpark.dao.dataobject.park.SmartParkCarParkPlaceExample;
import com.dianxin.smartpark.dao.mapper.park.SmartParkCarParkPlaceMapper;
import com.dianxin.smartpark.dao.qo.park.SmartParkCarParkPlaceQO;
import com.dianxin.smartpark.service.park.SmartParkCarParkPlaceService;
import com.dianxin.smartpark.service.validation.Delete;
import com.dianxin.smartpark.service.validation.Save;
import com.dianxin.smartpark.service.validation.Update;
import com.dianxin.smartpark.service.vo.park.SmartParkCarParkPlaceVO;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;
import org.springframework.validation.annotation.Validated;

import javax.annotation.Resource;

/**
 * 停车位业务层处理
 * 
 * @author chenjiasong
 * @date 2021-07-28
 */
@Service
public class SmartParkCarParkPlaceServiceImpl implements SmartParkCarParkPlaceService
{
    private final static Logger logger = LoggerFactory.getLogger(SmartParkCarParkPlaceServiceImpl.class);
    @Resource
    private SmartParkCarParkPlaceMapper smartParkCarParkPlaceMapper;

    /**
     * 查询停车位
     * 
     * @param qo 停车位ID
     * @return 停车位
     */
    @Override
    public CommonResponseBody<SmartParkCarParkPlaceVO> selectSmartParkCarParkPlace(SmartParkCarParkPlaceQO qo)
    {
        if(StringUtils.isNull(qo.getId())){
            return ResponseBodyWrapperUtil.fail(ErrMsgEnum.ERROR_PARAME);
        }
        SmartParkCarParkPlaceExample smartParkCarParkPlaceExample = new SmartParkCarParkPlaceExample();
        SmartParkCarParkPlaceExample.Criteria criteria = smartParkCarParkPlaceExample.createCriteria();
        if (qo.getTenantId() != null) {
            criteria.andTenantIdEqualTo(qo.getTenantId());
        }
        if (qo.getStatus() != null) {
            criteria.andStatusEqualTo(qo.getStatus());
        }
        if (qo.getPlaceType() != null) {
            criteria.andPlaceTypeEqualTo(qo.getPlaceType());
        }
        if (qo.getTenantId() != null) {
            criteria.andTenantIdEqualTo(qo.getTenantId());
        }
        criteria.andIsDeletedEqualTo(0);
        criteria.andIdEqualTo(qo.getId());
        List<SmartParkCarParkPlace> passRecords = smartParkCarParkPlaceMapper.selectByExample(smartParkCarParkPlaceExample);
        return ResponseBodyWrapperUtil.successWrapper(StringUtils.isNotEmpty(passRecords)?passRecords.get(0):null,SmartParkCarParkPlaceVO.class);
    }

    /**
     * 查询停车位列表
     * 
     * @param qo 停车位
     * @return 停车位
     */
    @Override
    public CommonResponseBody<List<SmartParkCarParkPlaceVO>> selectSmartParkCarParkPlaceList(SmartParkCarParkPlaceQO qo)
    {
        SmartParkCarParkPlaceExample smartParkCarParkPlaceExample = new SmartParkCarParkPlaceExample();
        SmartParkCarParkPlaceExample.Criteria criteria = smartParkCarParkPlaceExample.createCriteria();
        if (qo.getTenantId() != null) {
            criteria.andTenantIdEqualTo(qo.getTenantId());
        }
        if (qo.getAreaId() != null) {
            criteria.andAreaIdEqualTo(qo.getAreaId());
        }
        if (qo.getStatus() != null) {
            criteria.andStatusEqualTo(qo.getStatus());
        }
        if (qo.getPlaceType() != null) {
            criteria.andPlaceTypeEqualTo(qo.getPlaceType());
        }
        if (qo.getTenantId() != null) {
            criteria.andTenantIdEqualTo(qo.getTenantId());
        }
        if (qo.getPlaceSpecs() != null) {
            criteria.andPlaceSpecsEqualTo(qo.getPlaceSpecs());
        }
        criteria.andIsDeletedEqualTo(0);
        if(StringUtils.isNotEmpty(qo.getSerachText())) {
            smartParkCarParkPlaceExample.setSerachText(qo.getSerachText());
        }
        if (qo.getNeedPage()) {
            smartParkCarParkPlaceExample.setOrderByClause("gmt_create desc limit " + (qo.getPageIndex() - 1) * qo.getPageSize() + "," + qo.getPageSize());
        } else {
            smartParkCarParkPlaceExample.setOrderByClause("gmt_create desc");
        }
        List<SmartParkCarParkPlace> passRecords = smartParkCarParkPlaceMapper.selectByExample(smartParkCarParkPlaceExample);
        long count = smartParkCarParkPlaceMapper.countByExample(smartParkCarParkPlaceExample);
        return ResponseBodyWrapperUtil
                .successListWrapper(passRecords, count, qo, SmartParkCarParkPlaceVO.class);
    }

    /**
     * 新增停车位
     * 
     * @param vo 停车位
     * @return 结果
     */
    @Override
    @Validated(Save.class)
    public CommonResponseBody<Boolean> insertSmartParkCarParkPlace(SmartParkCarParkPlaceVO vo)
    {
        // 验证 todo
        SmartParkCarParkPlace smartParkCarParkPlace = new SmartParkCarParkPlace();
        try {
            BeanUtil.insertConversion(vo, smartParkCarParkPlace);
        } catch (Exception e) {
            logger.error("", e);
        }
        smartParkCarParkPlace.beforeInsert();
        return ResponseBodyWrapperUtil.toResData(smartParkCarParkPlaceMapper.insertSelective(smartParkCarParkPlace));
    }

    /**
     * 修改停车位
     * 
     * @param vo 停车位
     * @return 结果
     */
    @Override
    @Validated(Update.class)
    public CommonResponseBody<Boolean> updateSmartParkCarParkPlace(SmartParkCarParkPlaceVO vo)
    {
        SmartParkCarParkPlace smartParkCarParkPlace = new SmartParkCarParkPlace();
        try {
            BeanUtil.insertConversion(vo, smartParkCarParkPlace);
        } catch (Exception e) {
            logger.error("", e);
        }
        smartParkCarParkPlace.beforeUpdate();
        return ResponseBodyWrapperUtil.toResData(smartParkCarParkPlaceMapper.updateByPrimaryKeySelective(smartParkCarParkPlace));
    }

    /**
     * 删除停车位信息
     * 
     * @param vo 停车位
     * @return 结果08
     */
    @Override
    @Validated(Delete.class)
    public CommonResponseBody<Boolean> deleteSmartParkCarParkPlace(SmartParkCarParkPlaceVO vo)
    {
        // 验证 todo
        if(StringUtils.isNotEmpty(vo.getIds())){
            if(vo.getIds().split(",").length<=0){
                return ResponseBodyWrapperUtil.fail(ErrMsgEnum.ERROR_PARAME);
            }
        }else {
            return ResponseBodyWrapperUtil.fail(ErrMsgEnum.ERROR_PARAME);
        }
        SmartParkCarParkPlace smartParkCarParkPlace = new SmartParkCarParkPlace();
        try {
            BeanUtil.insertConversion(vo, smartParkCarParkPlace);
        } catch (Exception e) {
            logger.error("", e);
        }
        smartParkCarParkPlace.beforeDelete();
        smartParkCarParkPlace.setIds(Convert.toLongArray(vo.getIds()));
        return ResponseBodyWrapperUtil.toResData(smartParkCarParkPlaceMapper.updateByPrimaryKeySelective(smartParkCarParkPlace));
    }
}
